package com.cn.sys.main.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cn.sys.frame.util.SpringContextUtil;
import com.cn.sys.main.entity.SysDictionary;
import com.cn.sys.main.service.SysDictionaryService;

public class DictCache {

	private static Logger logger = LoggerFactory.getLogger(DictCache.class);
	
	private static Map<String, List<SysDictionary>> dictMapCache = new ConcurrentHashMap<String, List<SysDictionary>>();
	
	/**
	 * 根据父代码初始化对应的数据字典
	 * @param parentCode
	 * @return
	 */
	private static List<SysDictionary> initDict(String parentCode){
		List<SysDictionary> result = new ArrayList<SysDictionary>();
		if(StringUtils.isNotBlank(parentCode)){
			SysDictionaryService dictionaryService = SpringContextUtil.getBean("sysDictionaryService");
			try {
				result = dictionaryService.getDictByParentCode(parentCode);
				dictMapCache.put(parentCode, result);
			} catch (Exception e) {
				//e.printStackTrace();
				logger.error("缓存数据字典["+parentCode+"]出错,错误信息: ["+ e.getMessage()+"]");
			}
		}
		return result;
	}
	/**
	 * 根据父代码获取对应的数据字典
	 * @param parentCode
	 * @return
	 */
	public static List<SysDictionary> getByParentCode(String parentCode){
		List<SysDictionary> result = null;
		if(StringUtils.isNotBlank(parentCode)){
			result = dictMapCache.get(parentCode);
			if(result==null){
				result = initDict(parentCode);
			}
		}
		return result;
	}
	/**
	 * 根据代码和父代码获取对应的字典名称
	 * @param code
	 * @param parentCode
	 * @return
	 */
	public static String convertCodeToName(String code, String parentCode){
		if(StringUtils.isBlank(code)){
			return "";
		}
		if(StringUtils.isBlank(parentCode)){
			return code;
		}
		List<SysDictionary> list = getByParentCode(parentCode);
		String result = findCodeToName(code, list);
		if(result.equals(code)){//相等,说明在缓存数据中没有找到
			list = initDict(parentCode);//重新初始化对应的数据字典数据
			result = findCodeToName(code, list);
		}
		return result;
	}
	
	public static String findCodeToName(String code, List<SysDictionary> list){
		String name = code;
		if(list!=null){
			for (SysDictionary dict : list) {
				if(code.equals(dict.getCode())){
					name = dict.getName();
					break;
				}
			}
		}
		return name;
	}
	/**
	 * 根据值和父代码获取对应的字典名称
	 * @param value
	 * @param parentCode
	 * @return
	 */
	public static String convertValToName(String value,String parentCode){
		if(StringUtils.isBlank(value)){
			return "";
		}
		if(StringUtils.isBlank(parentCode)){
			return value;
		}
		List<SysDictionary> list = getByParentCode(parentCode);
		String result = findValToName(value, list);
		if(result.equals(value)){
			list = initDict(parentCode);
			result = findValToName(value, list);
		}
		return result;
	}
	
	public static String findValToName(String value, List<SysDictionary> list){
		String name = value;
		if(list!=null){
			for (SysDictionary dict : list) {
				if(value.equals(dict.getVal())){
					name = dict.getName();
					break;
				}
			}
		}
		return name;
	}
}
